package com.renhe.controller;

import com.renhe.pojo.Count;
import com.renhe.pojo.QueryVo;
import com.renhe.service.CustomerService;
import com.renhe.utils.EasyUIResult;
import com.renhe.utils.qrcode.QRCodeUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * Created by zhanglei on 2017/8/15.
 */
@Controller
public class CustomerController {

    //页面跳转
    @RequestMapping("{page}")
    public String getPage(@PathVariable String page) {
        return page;
    }

    @RequestMapping("/")
    public String getIndex() {
        return "index";
    }


    @Autowired
    private CustomerService customerService;

    //查询顾客列表
    @RequestMapping("/customer/customerList")
    @ResponseBody
    public EasyUIResult getCustomerList(QueryVo queryVo) {
        if(StringUtils.isBlank(queryVo.getBtime())) {
            getLastDay(queryVo);
        }
        EasyUIResult easyUIResult = customerService.getCustomerList(queryVo);
        return easyUIResult;
    }


    //查询北京和山海的人数
    @RequestMapping("/customer/getCount")
    @ResponseBody
    public Count getCount(QueryVo queryVo) {

        if(StringUtils.isBlank(queryVo.getBtime())) {
            getLastDay(queryVo);
        }

        Count count = customerService.getCount(queryVo);
        System.out.println(count.getCount1());
        System.out.println(count.getCount2());

        return count;
    }

    //生成二维码
    @RequestMapping("/getQRCode")
    public ResponseEntity<byte[]> getQRCode() {

        String Path = CustomerController.class.getClassLoader().getResource("icon.png").getPath();
        System.out.println("path:"+Path);
        ResponseEntity<byte[]> responseEntity = QRCodeUtil.zxingCodeCreate("大家好,字数太少会被logo盖住", 300, 300, "png", Path);
//        将图片输出
//        QRCodeUtil.zxingCodeCreate("大家好,字数太少会被logo盖住", 300, 300, "F:\\图片\\hahah.png","png", Path);
        return responseEntity;

    }




    //获取当前日期的前一天
    public void getLastDay(QueryVo queryVo) {
        Date date = new Date();
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        //当前日期的前一天
        calendar.add(calendar.DATE,-1);
        date = calendar.getTime();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String today = simpleDateFormat.format(date);
        queryVo.setBtime(today);
    }


}
